####################################################################################################################
#Paper title: Taking a Gamble: Chinese Overseas Energy Finance and Country Risk
#Authors: Hanna Niczyporuk (NYU) and Johannes Urpelainen (JOHNS HOPKINS SAIS) 

#Last modified: December 6, 2020

#Purpose: Generate summary statistics Table 1 
#Data used: mdb_energysources_orig_nocontrols.csv

####################################################################################################################

#First load all packages

library(foreign)
library(doBy)
library(ggplot2)
library(sf)
library(raster)
library(spData)
library(dplyr)
library(tidyr)  
library(xtable)
library(stringr)

####################################################################################################################

#Summary stats tables from the main body of the text

####################################################################################################################

#Table 1
mdb_cye_no_unspec <- read.csv("~/workingdirectory/mdb_energysources_orig_nocontrols.csv", header=TRUE, strip.white = TRUE)
mdb_cye_no_unspec<- filter(mdb_cye_no_unspec, mdb_cye_no_unspec$energy_source!="Unspecified Source")
mdb_cye_no_unspec<- filter(mdb_cye_no_unspec, mdb_cye_no_unspec$energy_source!="Thermal")
mdb_cye_no_unspec<- filter(mdb_cye_no_unspec, year<2018) 
mdb_cye_no_unspec<- filter(mdb_cye_no_unspec, 2004<year) 
mdb_cye_no_unspec$countryid <- as.numeric(as.factor(mdb_cye_no_unspec$country))

#Total by energy source per development bank
table1 <- mdb_cye_no_unspec %>%
  group_by(energy_source,mdb) %>% 
  summarise(mdb_finance_bln_usd = sum(mdb_finance_bln_usd), count = sum(count))

#Group solar, wind and biomass together
renewables <- c("Solar", "Biomass","Wind")
table1_b<- filter(table1, energy_source %in% renewables)
table1_b<- table1_b %>%
  group_by(mdb) %>% 
  summarise(mdb_finance_bln_usd = sum(mdb_finance_bln_usd), count = sum(count))
table1_b$energy_source <- "Solar, wind and biomass"
table1_b <- table1_b[c("energy_source", "mdb", "mdb_finance_bln_usd", "count")]

#Final summary stats
nonrenewable <- c("Coal", "Hydropower", "Nuclear", "Oil", "Gas")
table1 <- filter(table1, energy_source %in% nonrenewable)
table1 <- bind_rows(table1, table1_b)

energy_source <- c("Nuclear", "Nuclear", "Nuclear", "Nuclear","Nuclear")
mdb <- c("ADB", "AFDB", "EIB", "IADB", "WB")
mdb_finance_bln_usd <- c(0,0,0,0,0)
count <- c(0,0,0,0,0)
c <- data.frame(energy_source, mdb, mdb_finance_bln_usd, count)
table1 <- bind_rows(table1,c)
table1$mdb[which(table1$mdb == "adb")] <-"ADB"
table1$mdb[which(table1$mdb == "afdb")] <-"AFDB"
table1$mdb[which(table1$mdb == "ebrd")] <-"EBRD"
table1$mdb[which(table1$mdb == "wb")] <-"WB"
table1$mdb[which(table1$mdb == "eib")] <-"EIB"
table1$mdb[which(table1$mdb == "cdb_chexim")] <-"CDB/CHEXIM"
table1$mdb[which(table1$mdb == "iadb")] <-"IADB"

print(xtable(table1, type = "latex"), file = "~/workingdirectory/table1.tex",digits=c(0,0,0,2,0))




